package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.enums.AppointmentEnums;
import cn.wolfcode.enums.ConsumptionEnums;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IAppointmentService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;
    @Autowired
    private IAppointmentService appointmentService;


    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }

    @Override
    public Long saveFormAppointment(Long appointmentId) {
        //修改预约单对象的状态
        appointmentService.updateStatus(appointmentId,AppointmentEnums.CONSUME.getValue());
        //通过id得到对应的预约单对象
        Appointment appointment = appointmentService.get(appointmentId);
        //将预约单信息封装到结算单对象
        Consumption consumption=new Consumption();
        Date now = new Date();
        String cno =  DateUtil.format(now, "yyyyMMddHHmmss") + RandomUtil.randomNumbers(5);
        consumption.setCno(cno);
        //当前状态
        consumption.setStatus(ConsumptionEnums.CONSUME.getValue());
        //客户名称
        consumption.setCustomerName(appointment.getContactName());
        //客户电话
        consumption.setCustomerTel(appointment.getContactTel());
        //进店时间
        consumption.setCheckinTime(appointment.getAppointmentTime());
        //关联预约单
        consumption.setAppointmentAno(appointment.getAno());
        //店铺信息
        consumption.setBusiness(appointment.getBusiness());
        //创建人信息
        consumption.setCreateUser(UserContext.getCurrentUser());
        //创建结算单的时间
        consumption.setCreateTime(now);
        //插入数据库
        consumptionMapper.insert(consumption);
        //返回结算单的id
        return consumption.getId();
    }

    @Override
    public void updateStatus(Consumption consumption) {
        consumptionMapper.updateStatus(consumption);
    }


}
